﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmOrderManagement : Form
    {
        public frmOrderManagement()
        {
            InitializeComponent();

            PopulateGrid();
        }

        private void PopulateGrid()
        {
          //  dgvOrders.Rows.Clear();

            dgvOrders.AutoGenerateColumns = false;
           
            // Data property settings.
            dgvOrders.Columns[0].DataPropertyName = "PurchaseOrderId";
          
            dgvOrders.Columns[1].DataPropertyName = "PurchaseOrderNum";
            dgvOrders.Columns[2].DataPropertyName = "PoDate";
            dgvOrders.Columns[3].DataPropertyName = "PoType";
            dgvOrders.Columns[4].DataPropertyName = "PoInitiatedBy";
            dgvOrders.Columns[5].DataPropertyName = "PoRemarks";
            dgvOrders.Columns[6].DataPropertyName = "PoStatus";

            IList<PurchaseOrders> orderList = GetListOfOrders();

           
            dgvOrders.DataSource = orderList;
          
            

        }

        private IList<PurchaseOrders> GetListOfOrders()
        {
            IList<PurchaseOrders> orderList ;

            switch (cboStatus.Text)
            {
                case "Received":
                    {
                       orderList = PurchaseOrderDataControl.Instance.GetAllPurchaseOrders("Received");
                    }
                    break;
                case "Open":
                    {
                         orderList = PurchaseOrderDataControl.Instance.GetAllPurchaseOrders("Open");
                    }
                    break;

                default:
                    {
                        orderList = PurchaseOrderDataControl.Instance.GetAllPurchaseOrders(); 
                    }
                    
                    break;

            }
            
            return orderList;
        }

        /// <summary>
        /// returns index of selected rows
        /// </summary>
        /// <returns></returns>
        private List<int> GetSelectedOrders()
        {
            List<int> selectedRows = new List<int>();
   
            foreach (DataGridViewRow rw in  dgvOrders.SelectedRows)
            {
                selectedRows.Add(rw.Index);
            }

            return selectedRows;
        }

        private int  GetSelectedRow()
        {
            int selectedRow =0;
            if (dgvOrders.CurrentRow != null)
            {
                selectedRow = (int) dgvOrders.CurrentRow.Cells[0].Value;
            }

            return selectedRow;
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            

            foreach (int i in GetSelectedOrders())
            {
                    // here we need to grab the id and then delete it. 
                PurchaseOrders p = PurchaseOrderDataControl.Instance.GetPurchaseOrderById((int)dgvOrders.Rows[i].Cells[0].Value);
                PurchaseOrderDataControl.Instance.Delete(p); 
            
            }

            PopulateGrid();

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmOrders orders = new frmOrders();
            orders.MdiParent = this.MdiParent;
            orders.Show();
        }

        private void dgvOrders_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int selectedItem = GetSelectedRow();

            if (selectedItem == 0) return;

            frmOrders orders = new frmOrders(GetSelectedRow());
            orders.MdiParent = this.MdiParent;
            orders.Show();
        }

        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            // filter grid 
            PopulateGrid();
        }

       
    }
}
